/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import domain.model.file.Categoria;
import domain.model.file.Registro;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import util.J2meAppHelper;

/**
 *
 * @author ADM & Alexandre Pretyman
 */
public class DetalheRegistroForm extends Form implements AppForm, CommandListener {

    private Command cmdBack = new Command("Voltar", Command.BACK, 0);
    private Command discarCmd = new Command("Discar", Command.ITEM, 1);
    private Command mapaCmd = new Command("Mapa", Command.ITEM, 1);
    private StringItem phoneLbl = new StringItem("", "");
    private StringItem phoneNo1StrItm = new StringItem("", "");
    private StringItem phoneNo2StrItm = new StringItem("", "");
    private AppForm backForm;
    private Categoria backParameter;
    private Registro reg = null;

    public DetalheRegistroForm() {
        super("", null);
        addCommand(cmdBack);
        setCommandListener(this);
    }

    public void commandAction(Command arg0, Displayable arg1) {

        if (arg0.equals(cmdBack)) {
            J2meAppHelper.getInstance().changeForm(backForm, backParameter);
        }
        if (arg0.equals(discarCmd)) {
            J2meAppHelper.getInstance().changeForm(new DiscarForm(), new RegDetailVO(reg, backParameter));
        }
        if (arg0.equals(mapaCmd)) {
            J2meAppHelper.getInstance().changeForm(MapForm.getInstance(), new RegDetailVO(reg, backParameter));
        }
    }

    public Displayable getDisplyable() {
        return this;
    }

    public void initialize(Object parameter) {

        if (parameter instanceof RegDetailVO) {
            reg = ((RegDetailVO) parameter).getRegistro();
            backForm = new RegistroForm();
            backParameter = ((RegDetailVO) parameter).getBackCategoria();
            if (backParameter == null) {
                backForm = new BuscaForm();
            }
        }

        deleteAll();

        setTitle(reg.getNome());

        appendStringItem("Nome:", reg.getNome());
       // appendStringItem("", "\n");

        append(new ImageItem ("", reg.getImagem(),
                ImageItem.LAYOUT_NEWLINE_BEFORE/* | ImageItem.LAYOUT_VSHRINK*/, "", Item.PLAIN));

        appendStringItem("", reg.getResumo());
        appendStringItem("", reg.getTexto());
        
        appendStringItem("Rua:", reg.getEndereco());
        appendStringItem("Bairro:", reg.getBairro());
        appendStringItem("Numero:", reg.getEndNumero());
        appendStringItem("Cidade:", reg.getCidade());
        appendStringItem("Estado:", "--".equals(reg.getUF()) ? null : reg.getUF());

        appendTelefones();

        appendStringItem("Site:", reg.getSite());
        appendStringItem("Email:", reg.getEmail());
    }

    public void appendStringItem(String label, String conteudo){
        if (!isBlankOrNull(conteudo)) {
            StringItem st = new StringItem(label, conteudo);
            st.setLayout(StringItem.LAYOUT_NEWLINE_BEFORE);
            append( st);
        }
    }
    
    boolean isBlankOrNull(String str){
        if(str == null){
            return true;
        }
        if(str.trim().length() == 0){
            return true;
        }
        if(str.equals("null")){
            return true;
        }
        if(str.equals("")){
            return true;
        }
        if(str.equals(" ")){
            return true;
        }
        return false;
    }

    private void appendTelefones() {
        final boolean hasPhoneNo1 = true;
        boolean hasPhoneNo2 = false;
        hasPhoneNo2 = reg.getTelefone2().trim().length() > 0;
        removeCommand(discarCmd);
        if (hasPhoneNo1 || hasPhoneNo2) {
            if(!hasPhoneNo2 && hasPhoneNo1){

            }
            else if (!hasPhoneNo2) {
                // se não existir o segundo telefone, assinalar o texto do label de telefones no singular
                phoneLbl.setLabel("Telefone: ");
            } else {
                phoneLbl.setLabel("Telefones: ");
            }
            append(phoneLbl);
            if (hasPhoneNo1) {
                phoneNo1StrItm.setText(reg.getTelefone());
                append(phoneNo1StrItm);
            }
            if (hasPhoneNo2) {
                phoneNo2StrItm.setText(" " + reg.getTelefone2());
                append(phoneNo2StrItm);
            }
            addCommand(discarCmd);
        }
    }
}
